package com.zzz.gp.task;

import com.zzz.gp.bean.Gp;
import com.zzz.gp.bean.vo.GpQuery;
import com.zzz.gp.dao.GpMapper;
import com.zzz.gp.service.GpHistoryService;
import com.zzz.gp.utils.HttpRequestUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * @description:
 * @author: ZZZ
 * @time: 2021/12/7 15:57
 */
@Slf4j
@Component
public class TodayTask {
    @Value("${stat.today.enable}")
    private boolean enable;

    @Value("${clean.day}")
    private Integer cleanDay;

    @Autowired
    private GpHistoryService gpHistoryService;

    @Autowired
    private GpMapper gpMapper;

    @Scheduled(cron = "${stat.today.cron}")
    public void todayTask() {
        if (enable) {
            gpMapper.cleanGpHistory();
            DateTimeFormatter stf = DateTimeFormatter.ofPattern("yyyyMMdd");

            GpQuery gpQuery = new GpQuery();
            String startDate = stf.format(LocalDate.now().minusDays(cleanDay));
            String endDate = stf.format(LocalDate.now());
            gpQuery.setStartDate(startDate);
            gpQuery.setEndDate(endDate);
            gpHistoryService.batch2(gpQuery);

          /*  List<Gp> gpList = gpMapper.getCode();
            if (gpList.size() > 0) {
                String date = stf.format(LocalDate.now());
                String url = String.format("https://q.stock.sohu.com/hisHq?code=cn_%s&start=%s&end=%s", 601868, date, date);
                String result = HttpRequestUtil.httpRequest(url);
                if (result.length() > 10) {
                    gpHistoryService.batch(date, gpList);
                } else {
                    log.info("============stat.today.cron失败 {}================", date);
                }
            }*/
        }
    }
}
